package com.midipad.controls;

import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;

import com.midipad.R;

/**
 * A button, toggle or momentary 
 * @author Tim
 *
 */
public class Button extends MidiController {
	
	public static enum TYPES {TOGGLE, MOMENTARY};
	//midi subtilties...
	//toggle: sends 0 or MAX_MIDI_MSG alternatively on each touch
	//momentary sends MAX_MIDI_MSG when touched and 0 when released
	protected TYPES type;
	protected boolean isToggled = false;
	protected boolean needToFireMidi = false;

	/**
	 * 
	 * @param type TOGGLE or MOMENTARY
	 */
	public Button(Context context, int x, int y, int width, int height, int[] midiMessages, TYPES type) {
		super(context, 1, "BUTTON", x, y, width, height, midiMessages);
		//set up background
		this.setBackgroundDrawable(getResources().getDrawable(R.drawable.button));
		this.type = type;
			
	}
	
	//need to override the default mechanism as the button is special... :(
	@Override
	public void sendMidi(){
		if(needToFireMidi){
			super.sendMidi();
			needToFireMidi = false;
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
	}

	@Override
	public void updateValues(int x, int y, MotionEvent me) {
		switch(this.type){
			case TOGGLE:
				if(me.getAction()==MotionEvent.ACTION_DOWN){
					if(isToggled){
						this.setBackgroundDrawable(getResources().getDrawable(R.drawable.button));
						midiValues[0]=0;
						needToFireMidi = true;
						isToggled = false;
					}else{
						this.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_on));
						midiValues[0]=MAX_MIDI_MSG_VAL;
						isToggled = true;
						needToFireMidi=true;
					}
				}
				break;
			case MOMENTARY:
				if(me.getAction()==MotionEvent.ACTION_DOWN){
					this.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_on));
					midiValues[0]=MAX_MIDI_MSG_VAL;
					needToFireMidi=true;
				}else if(me.getAction()==MotionEvent.ACTION_UP || me.getAction()==MotionEvent.ACTION_CANCEL){
					this.setBackgroundDrawable(getResources().getDrawable(R.drawable.button));
					midiValues[0]=0;
					needToFireMidi=true;
				}
				break;
		}
		
	}

	//TODO:verif
	@Override
	public void setValues(int val1, int val2, int val3) {
		switch(this.type){	
			case TOGGLE:
				if(val3==0){//TODO:really 0?
					this.setBackgroundDrawable(getResources().getDrawable(R.drawable.button));
					midiValues[0]=0;//TODO:necessary?
					isToggled = false;
				}else{
					this.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_on));
					midiValues[0]=MAX_MIDI_MSG_VAL;//TODO:necessary?
					isToggled = true;
				}
				break;
			case MOMENTARY:
				if(val3==0){
					this.setBackgroundDrawable(getResources().getDrawable(R.drawable.button));
					midiValues[0]=0;
				}else{
					this.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_on));
					midiValues[0]=MAX_MIDI_MSG_VAL;
				}
				break;
		}
	}

}
